from PyQt6.QtWidgets import QWidget, QVBoxLayout, QPushButton
from PyQt6.QtCore import pyqtSignal

class MenuWidget(QWidget):
    # 定义页面切换信号
    page_switched = pyqtSignal(int)
    
    def __init__(self, parent=None):
        super().__init__(parent)
        self.init_ui()
        
    def init_ui(self):
        layout = QVBoxLayout()
        self.setLayout(layout)
        
        # 设置固定宽度
        self.setFixedWidth(200)
        
        # 创建菜单按钮
        self.buttons = []
        menu_items = [
            ("任务配置", 0),
            ("任务管理", 1),
            ("系统日志", 2),
            ("系统设置", 3)
        ]
        
        for text, index in menu_items:
            btn = QPushButton(text)
            btn.setCheckable(True)
            btn.clicked.connect(lambda checked, idx=index: self.on_button_clicked(idx))
            layout.addWidget(btn)
            self.buttons.append(btn)
            
        # 默认选中第一个按钮
        self.buttons[0].setChecked(True)
        
        # 添加弹性空间
        layout.addStretch()
        
        # 设置样式
        self.setStyleSheet("""
            QPushButton {
                text-align: left;
                padding: 10px 20px;
                border: none;
                background: transparent;
                font-size: 14px;
            }
            
            QPushButton:hover {
                background-color: #e0e0e0;
            }
            
            QPushButton:checked {
                background-color: #2196F3;
                color: white;
            }
        """)
        
    def on_button_clicked(self, index):
        # 更新按钮状态
        for i, btn in enumerate(self.buttons):
            btn.setChecked(i == index)
        
        # 发送页面切换信号
        from ..main_window import MainWindow
        if isinstance(self.parent(), MainWindow):
            self.parent().switch_page(index) 